package com.huiquan.framework.utils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class CollectionUtil {

	public static List<String> getListByArray(String[] array) {
		if (array == null) {
			return null;
		}
		List<String> list = new ArrayList<>();
		if (array.length > 0) {
			for (String s : array) {
				list.add(s);
			}
		}
		return list;
	}

	public static <T> String joinStringByList(List<T> collection, String separator) {
		if (collection == null) {
			return null;
		}

		Iterator<T> iterator = collection.iterator();

		// handle null, zero and one elements before building a buffer
		if (iterator == null) {
			return null;
		}
		if (!iterator.hasNext()) {
			return "";
		}
		T first = iterator.next();
		if (!iterator.hasNext()) {
			return first.toString();
		}

		// two or more elements.
		StringBuffer buf = new StringBuffer(256);
		if (first != null) {
			buf.append(first);
		}

		while (iterator.hasNext()) {
			if (separator != null) {
				buf.append(separator);
			}
			Object obj = iterator.next();
			if (obj != null) {
				buf.append(obj);
			}
		}
		return buf.toString();

	}

}
